//
//  GWDetailTopToolBar.m
//  新浪微博
//
//  Created by qianfeng on 16/2/25.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWDetailTopToolBar.h"
#import "GWStatuseModel.h"
@interface GWDetailTopToolBar ()

@property (nonatomic,weak)UIButton *reportBtn;
@property (nonatomic,weak)UIButton *commentBtn;
@property (nonatomic,weak)UIButton *likeBtn;
@property (nonatomic,weak)UIImageView *dividerView;
@property (nonatomic,weak)UIImageView *arrawView;

@property (nonatomic,weak)UIButton *selectedBtn;
@end
@implementation GWDetailTopToolBar

-(instancetype)initWithFrame:(CGRect)frame
{
    
    
    frame =CGRectMake(0, 0, GWScreenW, 45);
    if (self =[super initWithFrame:frame]) {
        [self setupBtn];
        self.backgroundColor =GWGlobalBackgroundColor;
        self.arrawView.y =self.height-self.arrawView.height;
        
        CGFloat btnW =90;
        CGFloat btnH =35;
        self.reportBtn.frame =CGRectMake(0, 10, btnW, btnH);
        self.commentBtn.frame =CGRectMake(btnW, 10, btnW, btnH);
        self.likeBtn.frame =CGRectMake(self.width-btnW, 10, btnW, btnH);
        
        self.dividerView.size =CGSizeMake(1, 20);
        self.dividerView.center =CGPointMake(btnW, self.reportBtn.center.y);
    }
    return self;
}
-(void)setupBtn
{
    self.reportBtn = [self btnWithTitle:@"转发" btnType:GWDetailTopToolBarBtnTypeReport];
    
   self.commentBtn =  [self btnWithTitle:@"评论" btnType:GWDetailTopToolBarBtnTypeComment];
   self.likeBtn =  [self btnWithTitle:@"赞" btnType:GWDetailTopToolBarBtnTypeLike];
    UIImageView *divider =[[UIImageView alloc]init];
    divider.image =[UIImage imageNamed:@"statusdetail_comment_top_rule"];
    [self addSubview:divider];
    self.dividerView =divider;
    
    UIImageView *arrawView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"statusdetail_comment_top_arrow"]];
    [self addSubview:arrawView];
    self.arrawView =arrawView;
}
-(UIButton *)btnWithTitle:(NSString *)title btnType:(GWDetailTopToolBarBtnType)btnType
{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    btn .titleLabel.font =[UIFont systemFontOfSize:13];
    btn.tag =btnType;
    [self addSubview:btn];
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}
-(void)btnOnClick:(UIButton *)btn
{
    self.selectedBtn.selected =NO;
    btn.selected =YES;
    self.selectedBtn =btn;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.arrawView.centerX =btn.centerX;
    }];
    
    
    if ([self.delegate respondsToSelector:@selector(detailTopToolBar:didSelecteBtnType:)]) {
        [self.delegate detailTopToolBar:self didSelecteBtnType:(GWDetailTopToolBarBtnType)btn.tag];
    }
}
-(void)setDelegate:(id<GWDetailTopToolBarDelegate>)delegate
{
    _delegate =delegate;
    [self btnOnClick:self.commentBtn];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
}
-(void)setStatuse:(GWStatuseModel *)statuse
{
    _statuse =statuse;
    
    
    [self setUpBtn:self.reportBtn setTitle:@"转发" WithCount:statuse.reposts_count];
    [self setUpBtn:self.commentBtn setTitle:@"评论" WithCount:statuse.comments_count];
    [self setUpBtn:self.likeBtn setTitle:@"赞" WithCount:statuse.attitudes_count];
    
}
-(void)setUpBtn:(UIButton *)btn setTitle:(NSString *)title WithCount:(int)count
{
    if (count>=10000) {
        title =[NSString stringWithFormat:@"%.1f万 %@",count/10000.0,title];
        title =[title stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }
    else if(count>0)
    {
        title =[NSString stringWithFormat:@"%d %@",count,title];
    }
    else
    {
        title =[NSString stringWithFormat:@"0 %@",title];
    }
    [btn setTitle:title forState:UIControlStateNormal];
}
-(void)drawRect:(CGRect)rect
{
    rect.origin.y =10;
    [[UIImage reSizeWithName:@"statusdetail_comment_top_background"]drawAsPatternInRect:rect];
}
@end
